# -*- coding: utf-8 -*-

import time
from elasticsearch import Elasticsearch, helpers
import redis
import configs

es = Elasticsearch(configs.es_source_host)

start_ts = time.time()

scroll_time = "10m"

r = redis.Redis(
    host=configs.redis_host,
    port=configs.redis_port,
    password=configs.redis_pass,
    db=configs.redis_db,
    socket_connect_timeout=2,
    decode_responses=True,
)

query = {
    "query": {
        "match_all": {}
    }
}

response = es.search(index=configs.es_source_index, scroll=scroll_time, body=query, size=configs.batch_size)
scroll_id = response['_scroll_id']
print("scroll_id -->", scroll_id)

count = 0

for hit in helpers.scan(es, query=query, index=configs.es_source_index, scroll=scroll_time,size=configs.batch_size):
    _id = hit["_id"]
    # print(_id)

    r.lpush(configs.redis_key, _id)
    count += 1

stop_ts = time.time()
print(f'scroll 遍历的总条数： {count} 耗时(秒): {int(stop_ts - start_ts)}')
